package net.betou.trade.action;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import net.betou.common.util.TradeUtils;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Notice;
import net.betou.trade.main.manager.ConfigMng;
import net.betou.trade.main.manager.NoticeMng;

import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 叶装装 列表的颜色设置： 自己的颜色： #FF6600 别人的颜色：
 * 
 */
@Controller
public class WelcomeAct{

	@RequestMapping( "/top.jspx" )
	public String top( HttpServletRequest request, ModelMap model ) {
		setConfig( model, request );
		return "top";
	}

	@RequestMapping( "/left.jspx" )
	public String left( HttpServletRequest request, ModelMap model ) {
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		model.addAttribute( "admin", admin );
		return "left";
	}

	/**
	 * 进入主页面
	 * 
	 * @return
	 */
	@RequestMapping( "/index.jspx" )
	public String index( HttpServletRequest request, ModelMap model ) {
		setConfig( model, request );
		return "index";
	}

	/**
	 * 首先欢迎信息
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping( "/main.jspx" )
	public String main( HttpServletRequest request, ModelMap model ) {

//		if( admin.getSystem( ) ){
			Properties props = System.getProperties( );
			model.addAttribute( "props", props );

			Runtime runtime = Runtime.getRuntime( );
			long freeMemoery = runtime.freeMemory( );
			long totalMemory = runtime.totalMemory( );
			long usedMemory = totalMemory - freeMemoery;
			long maxMemory = runtime.maxMemory( );
			long useableMemory = maxMemory - totalMemory + freeMemoery;
			model.addAttribute( "freeMemoery", freeMemoery );
			model.addAttribute( "totalMemory", totalMemory );
			model.addAttribute( "usedMemory", usedMemory );
			model.addAttribute( "maxMemory", maxMemory );
			model.addAttribute( "useableMemory", useableMemory );
//		}
		
		setConfig( model, request );
		
		popNotice( request, model );

		return "main";
	}

	private void popNotice( HttpServletRequest request, ModelMap model ) {
		Notice notice = mng.getLastNotice( );
		if( notice == null ){
			return;
		}
		String title = Jsoup.clean( notice.getTitle( ), Whitelist.none( ) );

		if( title.length( ) > 30 ){
			title = title.substring( 0, 30 );
		}

		notice.setTitle( title );

		model.addAttribute( "notice", notice );

	}
	
	private void setConfig( ModelMap model, HttpServletRequest request ){
		AgentAndAdmins admin = TradeUtils.getAdmin( request );
		model.addAttribute( "config", configMng.findConfig( admin ) );
		
		model.addAttribute( "admin", admin );
	}
	
	@Autowired
	private ConfigMng configMng;

	@Autowired
	private NoticeMng mng;
}
